//
//  SSUserManager.m
//  wordSmartStore
//
//  Created by xieShou on 2020/4/29.
//  Copyright © 2020 xieShou. All rights reserved.
//

#import "SSUserManager.h"
#import "SSUntil.h" 
@implementation SSUserManager
+ (instancetype)sharedUser {
    static SSUserManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SSUserManager alloc] init];
    });
    return manager;
}
- (BOOL)isLogin {
    
    NSString *tempTkStr = [[NSUserDefaults standardUserDefaults] valueForKey:@"token"];
    NSLog(@"tempTkStr==%@\n",tempTkStr);
    if (notNillStr(tempTkStr)) {
        NSString *tempStr = [[[NSUserDefaults standardUserDefaults] valueForKey:@"token"] stringByReplacingOccurrencesOfString:@" " withString:@""];
//        NSString *tempStr2 = [NSString stringWithFormat:@"bearer %@",@""];
        if([tempStr isEqualToString:@"bearer"]){
            self.isLoginSuccess = NO;
            return NO;
        }
        self.isLoginSuccess = YES;
        return YES;
    }
    self.isLoginSuccess = NO;
    return NO;
}
- (void)getUserInfoSuccess:(void (^)(id _Nonnull data))success
{
    
    
   XSWeakSelf;
   NSMutableDictionary *paramMdic = [[NSMutableDictionary alloc] init];
   NSLog(@"API_getUserInfo--paramMdic==%@\n",paramMdic);// application/x-www-form-urlencoded
//    [[XLNetWorkManager shareInstance] GETBody:API_getUserInfo port:API_NOTPORT_31006 withrequestSerializerContentType:@"application/x-www-form-urlencoded" parameters:paramMdic success:^(id _Nonnull responseObject) {
//       
//       NSLog(@"API_getUserInfo--reeyeyrettt1we1responseObject==%@\n",responseObject);
//       if ([responseObject[@"code"] integerValue] == 200) {
//           
//           NSString *IsString = [NSString stringWithFormat:@"%ld",[responseObject[@"data"][@"id"] integerValue]];
//           SSUserInfoModel *userInfo = [SSUserInfoModel yy_modelWithJSON:responseObject[@"data"]];
//           userInfo.Id = IsString;
//           userInfo.codeRelease = [responseObject[@"data"][@"release"] intValue];
//           
//           if(notNillStr(userInfo.headImg)){
//               if([userInfo.headImg containsString:@"http://"]||[userInfo.headImg containsString:@"https://"]){
//                   userInfo.headImg = [NSString stringWithFormat:@"%@",userInfo.headImg];
//               }
//               else{
//                   userInfo.headImg = [NSString stringWithFormat:@"%@%@",API_Images_BASEURL,userInfo.headImg];
//               }
//               
//           }
//           userInfo.accesstokenCode = accesstokenCodeNum;
//           
//           [[SSUserManager sharedUser] setUserModel:userInfo];
////           //12.12. 获取溯源设置
////           [weakSelf getTraceSourceSettingAction];
//
//           success(responseObject);
//           [weakSelf getRootCloudNodeSuccess:^(id  _Nonnull data) {
//
//            }];
//       }
//       else{
//#warning 暂时注释
//#warning 暂时注释
//#warning 暂时注释
//#warning 暂时注释
//#warning 暂时注释
////            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
//       }
//   } failure:^(NSError * _Nonnull error) {
//       NSLog(@"API_getUserInfo--error==%@\n",error.description);
////       [MBProgressHUD showError:error.description];
//   }];
    
}
//{
//
//   XSWeakSelf;
//   NSMutableDictionary *paramMdic = [[NSMutableDictionary alloc] init];
//   NSLog(@"API_getUserInfo--paramMdic==%@\n",paramMdic);// application/x-www-form-urlencoded
//    [[XLNetWorkManager shareInstance] GETBody:API_getUserInfo port:API_NOTPORT_31006 withrequestSerializerContentType:@"application/x-www-form-urlencoded" parameters:paramMdic success:^(id _Nonnull responseObject) {
//
//       NSLog(@"API_getUserInfo--645yherresponseObject==%@\n",responseObject);
//       if ([responseObject[@"code"] integerValue] == 200) {
//           SSUserInfoModel *userInfo = [SSUserInfoModel yy_modelWithJSON:responseObject[@"data"]];
//           [SSUserManager sharedUser].isLoginSuccess = YES;
//           [SSUserManager sharedUser].userModel = userInfo;
//           userInfo.codeRelease = [responseObject[@"data"][@"release"] intValue];
//           SSUserInfoModel *tempModel = [SSUserManager sharedUser].userModel;
//           int a = 10;
////           [[NSNotificationCenter defaultCenter] postNotificationName:KUserInfoUpdateNotification object:nil];
//           success(responseObject);
//           [self getRootCloudNodeSuccess:^(id  _Nonnull data) {
//
//             }];
//       }
//       else{
//#warning 暂时注释
//#warning 暂时注释
//#warning 暂时注释
//#warning 暂时注释
//#warning 暂时注释
////            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"token"];
//       }
//
//   } failure:^(NSError * _Nonnull error) {
//       NSLog(@"API_getUserInfo--error==%@\n",error.description);
//   }];
//
//
//
//}
- (void)getUserInfo {
    [self getUserInfoSuccess:^(id _Nonnull data) {
        
    }];
}

- (void)getRootCloudNodeSuccess:(void (^)(id _Nonnull))success {
    if (![SSUserManager sharedUser].isLogin) {
        return;
    }
//    [[SSDocManager shared] getFloderList:1 pageSize:10 success:^(SSNodeData *  _Nonnull data) {
//        SSNode *node = data.data.node;
//        node.filePath = node.filename;
//        [SSDocManager shared].cloudRootNode = node;
//
//        [[NSUserDefaults standardUserDefaults] setObject:@(data.data.node.id) forKey:@"cloudRootNodeID"];
//        [[NSUserDefaults standardUserDefaults] synchronize];
//        success(data);
//    }];
}
- (NSInteger)cloudRootNodeID {
    if ([[[NSUserDefaults standardUserDefaults] valueForKey:@"cloudRootNodeID"] integerValue]) {
        return [[[NSUserDefaults standardUserDefaults] valueForKey:@"cloudRootNodeID"] integerValue];
    }else{
        return 0;
    }
}

- (void)checkMobile:(NSString *)mobile success:(nonnull void (^)(id _Nonnull))success fail:(nonnull void (^)(id _Nonnull))fail {
    [[XLNetWorkManager shareInstance] POST:API_CheckMobile parameters:@{@"mobile":mobile} success:^(id  _Nonnull responseObject) {
                    success(responseObject);

    } failure:^(NSError * _Nonnull error) {
        fail(error);
    }];
}

- (void)sendSmsCode:(NSString *)mobile type:(NSInteger)type success:(nonnull void (^)(id _Nonnull))success fail:(nonnull void (^)(id _Nonnull))fail{
    NSDictionary *paramter = @{@"mobile":mobile,@"type":@(type)};
    [[XLNetWorkManager shareInstance] POST:API_SendSms parameters:paramter success:^(id  _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 0  ) {
            success(@([responseObject[@"data"][@"codeId"] integerValue]));
        }else{
            fail(responseObject[@"msg"]);
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
- (void)toLoginSuccess:(void (^)(id _Nonnull))success{
//    SSLoginViewController *loginVc = [SSLoginViewController new];
//    loginVc.loginSuccess = ^(id  _Nonnull data) {
//        success(data);
//    };
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVc];
//    nav.modalPresentationStyle = 0;
//    
//    [[SSUntil getCurrentVC] presentViewController:nav animated:YES completion:nil];

}
- (void)loginOut:(void (^)(id data))success {
    [[XLNetWorkManager shareInstance] POST:API_LoginOut parameters:@{} success:^(id  _Nonnull responseObject) {
        success(responseObject);
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
@end
